/***************************************************************************
 *   Copyright (C) 2004 by cyril dupuit                                    *
 *   cyrildupuit@hotmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

//*****************************************************************************
// Nom du module : TimeInfo.c
// Description :  Obtenir des infos sur les timers.
//*****************************************************************************
 
// Historique :
// 28/10/04 : Creation du module V1.0  

#include "Kernel/Core.h"
#include "Misc/Console.h"
#include "Lib/Queue.h"
#include "Misc/TimeInfo.h"
#include "Kernel/Schedule.h"
#include "Arch/Pentium/Kernel/Cpu.h"
#include "Lib/StdLib/stdio.h"
#include "Kernel/DynTimer.h"

#include "Kernel/Errors.h"

//*****************************************************************************
// But de la fonction : Afficher une liste de tous les timers crees
// Entree : Aucune
// Parametre retourne : TRUE si liste affichee, FALSE si liste est vide, ERROR
// si memoire insuffisante
//*****************************************************************************
Status_t TimerListShow(void)
{
	QueueID_t QueueTimerList;
	TimerID_t TimerID;
	unsigned int Line = 0;
	Coord_t X, Y;
	unsigned int TimerNbr;
	Ticks_t Expire, Jiffies;
	
	TimerNbr = TimerCount();
	if(TimerNbr == 0)
	{
		if(printf("Aucun Timer cree.\n") == -1) return ERROR;
		return FALSE;	
	}
	QueueTimerList = QueueCreate(TimerNbr, QUEUE_FIFO);
	if(QueueTimerList == 0)
	{//Creation de la file impossible
		if(printf("Memoire insuffisante pour afficher la liste des Timers.\n") == -1) return ERROR;
		SetLastError(INSUFFICIENT_MEMORY_AVAILABLE);
		return ERROR;
	}
	else
	{//Creation reussie	
		if(printf("Liste des Timers crees : \n") == ERROR) return ERROR;
		if(TimerGetList(QueueTimerList) == FALSE)
		{//Un semaphore a ete cree entre temps, la file d'attente est trop petite
			if(printf("La liste ci-dessous est incomplete, un Timer vient d'etre cree ou un debordement de pile a eu lieu !\n") == -1) return ERROR;	
		}
		if(ConsoleGetCursorPosition(&X, &Y) == ERROR) return ERROR;
		if(printf("ID : ") == -1) return ERROR;
		GotoXY(X + 15, Y);//Se placer a la ligne courante au milieu de l'ecran
		if(printf("Jiffies : ") == -1) return ERROR;
		GotoXY(X + 30, Y);//Se placer a la ligne courante au milieu de l'ecran
		if(printf("Expire :\n") == -1) return ERROR;
		//Affichage des parametres des taches
		TimerID = (TimerID_t)QueueRead(QueueTimerList);
		while(TimerID != 0)
		{//Afficher la liste des taches declarees
			if(ConsoleGetCursorPosition(&X, &Y) == ERROR) return ERROR;
			//Afficher le numero du semaphore
			GotoXY(X , Y);
			if(printf("%ld", TimerID) == -1) return ERROR;
			//Recuperer les infos concernant ce timer
			if(TimerGetExpire(TimerID, &Expire, &Jiffies) == FALSE) return ERROR;
			//Afficher le temps noyau
			GotoXY(X + 15, Y);
			if(printf("%ld", Jiffies) == -1) return ERROR;
			//Afficher l'echeance du timer
			GotoXY(X + 30, Y);
			if(printf("%ld\n", Expire) == -1) return ERROR;

			//Recuperer le prochain numero du semaphore
			TimerID = (TimerID_t)QueueRead(QueueTimerList);
			Line = (Line + 1) % 20;//20 equivaut au nombre de ligne sur la console
			if((Line == 0)&&(TimerID != 0))
			{//Demander a l'utilisateur de presser une touche pour voir la suite de l'ecran
				if(Pause() != TRUE)
				{//L'utilisateur stoppe la visualisation
					QueueDelete(&QueueTimerList);
					
					return TRUE;
				}
			}
		}
	}
	QueueDelete(&QueueTimerList);
	
	return TRUE;
}

//*****************************************************************************
